#include "Entity.h"

using namespace std;
using namespace me;


///////////////////////////////////////////////////////
//Standard Entity
///////////////////////////////////////////////////////

Entity::Entity(me::Vector2f pos, Vector2f scale, sf::Texture* texture, sf::Color color):
    m_world_pos(pos)
{
	this->setTexture(*texture);
	this->setPosition(sf::Vector2f(pos.x, pos.y));
	this->setColor(color);
	this->setScale(sf::Vector2f(scale.x, scale.y));
}

Entity::~Entity()
{
}


me::Vector2f Entity::getWorldPosition()
{
    return m_world_pos;
}

cpBody* Entity::getBody()
{
	return NULL;
}

vector<cpShape*> Entity::getShapes()
{
	vector<cpShape*> ret;
	return ret;
}
vector<cpConstraint*> Entity::getConstraints()
{
	vector<cpConstraint*> ret;
	return ret;
}



void Entity::setWorldPosition(me::Vector2f pos)
{
    m_world_pos = pos;
}

void Entity::moveWorldPosition(Vector2f movement)
{
    m_world_pos += movement;
}

void Entity::update()
{
    AnimatedSprite::update();
}

void Entity::addShape(cpShape* shape)
{

}
void Entity::addConstraint(cpConstraint* constraint)
{

}


///////////////////////////////////////////////////////
//Pysical Entity
///////////////////////////////////////////////////////


PhysicalEntity::PhysicalEntity(cpBody* body,
							me::Vector2f pos, me::Vector2f scale, sf::Texture* texture, sf::Color color):
								Entity(pos, scale, texture, color), m_body(body)
{
	cpBodySetPos(m_body, cpv(pos.x, pos.y));
}

PhysicalEntity::~PhysicalEntity()
{
	cpBodyFree(m_body);
	for (unsigned int i=0 ; i < m_shapes.size() ; i++)
		cpShapeFree(m_shapes[i]);
}

void PhysicalEntity::setWorldPosition(me::Vector2f pos)
{
	m_world_pos = pos;
	cpBodySetPos(m_body, cpv(pos.x, pos.y));
}

void PhysicalEntity::update()
{
    Entity::update();
	cpVect pos = cpBodyGetPos(m_body);
	m_world_pos.x = pos.x;
	m_world_pos.y = pos.y;
	cpFloat angle = cpBodyGetAngle(m_body) * 180.0f/PI;
	this->setRotation(angle);
}

void PhysicalEntity::addShape(cpShape* shape)
{
	cpSpace* space = cpBodyGetSpace(m_body);
	cpSpace* sSpace = cpShapeGetSpace(shape);
	if (space != sSpace)
        cpSpaceAddShape(space, shape);
	m_shapes.push_back(shape);
}

void PhysicalEntity::addConstraint(cpConstraint* constraint)
{
	cpSpace* space = cpBodyGetSpace(m_body);
	cpSpaceAddConstraint(space, constraint);
	m_constraints.push_back(constraint);
}

cpBody* PhysicalEntity::getBody()
{
	return m_body;
}

vector<cpShape*> PhysicalEntity::getShapes()
{
	return m_shapes;
}

vector<cpConstraint*> PhysicalEntity::getConstraints()
{
	return m_constraints;
}
